<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Game\GameChannel;
use App\Models\Game\GamePackage;
use App\Models\Game\VersionEvent;
use App\Services\VersionEventService;
use App\Utils\ResponseUtil;

class VersionEventController extends Controller
{
    private VersionEventService $versionEventService;

    /**
     * VersionEventController constructor.
     * @param VersionEventService $versionEventService
     */
    public function __construct(VersionEventService $versionEventService)
    {
        $this->versionEventService = $versionEventService;
    }

    public function add(){
        $data = request()->only(['model','andriod_url','ios_url','pc_url','package_model','message','comment','channel_id','content_json','version','is_sh','age']);
        $data['created_at'] = now();
        $packageModel = data_get($data,'package_model');
        if ($packageModel==1){ //指定包
            $packageNos = array_values(request()->get('packageNos',[]));
            $data['package_nos'] = json_encode($packageNos);
        } else { //全部包
            $channelId = request()->get('channel_id');
            $gameChannel = GameChannel::query()->with('packages')->find($channelId);
            $packageNoList = [];
            foreach($gameChannel->packages as $package){
                $packageNoList[] = $package->package_no;
            }
            $data['package_nos'] = json_encode($packageNoList);
        }

        VersionEvent::query()->insert($data);
        $this->versionEventService->refreshPackageNos();
        return ResponseUtil::success();
    }

    public function delete($id){
        VersionEvent::query()->where('id',$id)->delete();
        return ResponseUtil::success();
    }

    public function edit($id){

        $data = request()->only(['model','andriod_url','ios_url','pc_url','package_model','message','comment','channel_id','content_json','version','is_sh','age']);
        $data['updated_at'] = now();

        $packageModel = data_get($data,'package_model');
        if ($packageModel==1){ //指定包
            $packageNos = array_values(request()->get('packageNos',[]));
            $data['package_nos'] = json_encode($packageNos);
        } else { //全部包
            $channelId = request()->get('channel_id');
            $gameChannel = GameChannel::query()->with('packages')->find($channelId);
            $packageNoList = [];
            foreach($gameChannel->packages as $package){
                $packageNoList[] = $package->package_no;
            }
            $data['package_nos'] = json_encode($packageNoList);
        }
        VersionEvent::query()->where('id',$id)->update($data);
        $this->versionEventService->refreshPackageNos();
        return ResponseUtil::success();
    }

    public function list()
    {
        $list = VersionEvent::query()->with('channel')->get()->toArray();
        foreach($list as &$item){
            $nos = json_decode(data_get($item,'package_nos'),true);
            $packages = GamePackage::query()->whereIn('package_no',$nos)->get();
            $item['packages'] = $packages;
            $item['package_nos'] = json_decode($item['package_nos'],true);
        }
        return ResponseUtil::success($list);
    }

    public function detail($id){
        $versionEvent = VersionEvent::query()->with('channel')->find($id);
        $nos = json_decode($versionEvent->package_nos,true); //list
        $packages = GamePackage::query()->whereIn('package_no',$nos)->get();
        $versionEvent['packages'] = $packages;
        $versionEvent['package_nos'] = json_decode($versionEvent['package_nos'],true);
        return ResponseUtil::success($versionEvent);
    }

}
